<?php 
/**
 * Qiaiqia 核心类
 * ============================================================================
 * * 版权所有 2014- 广州维赛网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.weisai.net；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 
 * $Author: hungry $
 * $Id: Qiaqia.class.php 17217 2014Z hungry $
 */

Class Qiaqia
{
	private static $_instance = array();
	
	/**
	 * 开始运行
	 * @access private
	 * @return string
	 * @author Hungry
	 */
	public static function start()
	{
		session(array());//开启session
		self::$_instance['core_class']=array('Error','StaticPage','Page');
		spl_autoload_register(array('Qiaqia', 'autoload'));
		
			
		
		$error=new Error();
		$error->run();
		self::loadlanguage();
		self::loadcfg();
		self::run();
		
	}
	/**
	 * 加载类
	 * @access private
	 * @return void
	 */
	private static function run()
	{
		$target=ROOT_PATH.GROUP_NAME.'/Controller/'.MODULE_NAME."Controller.php";
		
		if (file_exists($target))
		{
		
			include_once($target);
		
		
			$class = MODULE_NAME."Controller";
		
			if (class_exists($class))
			{
				$controller = new $class;
			}
			else
			{
				hy_404();
				//die('class does not exist!');
			}
		
			if(ACTION_NAME)
			{
				if(!method_exists($controller,ACTION_NAME))
				{
					hy_404();
					//die('The method does not exist');
				}
				else
				{
					$action=ACTION_NAME;
					$controller->$action();
				}
			}
		}
		else
		{
			hy_404();
			//die('page does not exist!');
		}
		
		
	}
	/**
	 * 加载配置与方法
	 * @access private
	 * @return void
	 */
	private static function loadcfg()
	{
		// 加载项目配置文件
		if(is_file(CFG_PATH.'/config.php'))
			C(include CFG_PATH.'/config.php');
		if(is_file(HOME_PATH.'/Config/config.php'))
			C(include HOME_PATH.'/Config/config.php');
		if(is_file(ADMIN_PATH.'/Config/config.php'))
			C(include ADMIN_PATH.'/Config/config.php');
		if(is_file(ADMIN_PATH.'/Common/commmon.php'))
			include ADMIN_PATH.'/Common/commmon.php';
	}
	/**
	 * 加载语言
	 * @access private
	 * @return void
	 */
	private static function loadlanguage()
	{
		
		$lang = $group_lang = $module_lang = array();
		$lang_dir = LANG_PATH . "/".DEFAULT_LANG;
	
		// 读取项目公共语言包
		if (is_file($lang_dir . '/common.php')) {
			$lang = include $lang_dir . '/common.php';
		}
		// 读取当前分组公共语言包
		if (defined('GROUP_NAME'))
		{
			$group_lang_file = $lang_dir . '/' . GROUP_NAME . '/common.php';
			if (is_file($group_lang_file)) {
				$group_lang = include $group_lang_file;
			}
		}
	
		// 读取当前模块语言包
		$module_lang_file = $lang_dir . '/' . GROUP_NAME . '/' . MODULE_NAME . '.php';
		
		if (is_file($module_lang_file)) {
			$module_lang = include $module_lang_file;
		}
		 
		$lang = array_merge($lang, $group_lang, $module_lang);
		Lang($lang);
	}
	
	private static function autoload($className)
	{
		
		$file='';
		if(substr($className,-5)=='Model')
		{
			if(defined('GROUP_NAME'))
			{
					
				$file = ROOT_PATH .'/'.GROUP_NAME.'/Model/' . $className . '.php';
			}
		}
		elseif(substr($className,-10)=='Controller'){ // 加载控制器
			if(defined('GROUP_NAME'))
			{
					
				$file = ROOT_PATH .'/'.GROUP_NAME.'/Controller/' . $className . '.php';
			}
		}
	
	
	
		//获取文件
		if (file_exists($file))
		{
	
			//引入文件
			include_once($file);
		}
		else
		{
			self::try_again($className);
	
	
		}
		
		
	}
	
	private static function try_again($className)
	{
	
		if(substr($className,-5)=='Model')
		{
			if(defined('GROUP_NAME'))
			{
	
				$file = CORE_PATH .'/Model/' . $className . '.php';
					
			}
		}
		elseif(substr($className,-10)=='Controller'){ // 加载控制器
	
	
			$file = CORE_PATH .'/Controller/' . $className . '.php';
	
		}
		elseif(substr($className,-2)=='DB'){ // 加载控制器
	
	
			$file = CORE_PATH .'/Database/' . $className . '.php';
	
		}
		elseif(in_array($className,self::$_instance['core_class']))//核心类
		{
			$file = CORE_PATH.'/Core/'.$className.'.class.php';
		}
		elseif(substr($className,0,6)=='Smarty')
		{
			$file = CORE_PATH .'/Vendor/' . substr($className,0,6) . '/'. substr($className,0,6) . '.class.php';
	
		}
		else//第三方类
		{
			$file = CORE_PATH .'/Vendor/' . $className . '/'. $className . '.class.php';
		
		}
		
		if (file_exists($file))
		{
	
			//引入文件
			include_once($file);
	
		}
		else
		{
	
			//文件不存在
			//die(" class '$className' not found.");
	
		}
		
	}
}